package com.gzz.manage.base.customer;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.gzz.common.aop.AutoLog;
import com.gzz.common.base.Page;
import com.gzz.common.config.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @类说明 【客户信息】控制器
 * @author 高振中
 * @date 2021-03-21 11:03:35
 **/
//@Slf4j
@RestController
@RequestMapping("customer")
@Api(tags = "【客户信息】API")
public class CustomerController {

	@Autowired
	private CustomerService customerService; // 注入【客户信息】业务逻辑层

	/**
	 * @方法说明 【客户信息】新增
	 */
	@PostMapping("save")
	@ApiOperation(value = "【客户信息】新增" , notes = "【客户信息】新增")
	@AutoLog("【客户信息】新增")
	public Result<Integer> save(@RequestBody @Validated Customer customer) {
		//if (customerService.count(CustomerCond.builder().nameEq(customer.getName()).build()) > 0)
			//return Result.error(1, "【客户信息名称】不能重复！");
		return Result.success(customerService.save(customer));
	}

	/**
	 * @方法说明 【客户信息】删除
	 */
	@PostMapping("delete")
	@ApiOperation(value = "【客户信息】删除" , notes = "参数格式:[1,2,3,4...]")
	@AutoLog("【客户信息】删除")
	public Result<Integer> delete(@RequestBody Object[] ids) {
		return Result.success(customerService.delete(ids));
	}

	/**
	 * @方法说明 【客户信息】修改
	 */
	@PostMapping("update")
	@ApiOperation(value = "【客户信息】修改" , notes = "【客户信息】修改")
	@AutoLog("【客户信息】修改")
	public Result<Integer> update(@RequestBody @Validated Customer customer) {
		//if (customerService.count(CustomerCond.builder().nameEq(customer.getName()).idNe(customer.getId()).build()) > 0)
			//return Result.error(1, "【客户信息名称】不能重复！");
		return Result.success(customerService.update(customer));
	}

	/**
	 * @方法说明 【客户信息】分页列表
	 */
	@PostMapping("page")
	@ApiOperation(value = "【客户信息】分页列表" , notes = "【客户信息】分页列表")
	@AutoLog("【客户信息】分页列表")
	public Result<Page<Customer>> page(@RequestBody CustomerCond cond) {
		return Result.success(customerService.page(cond));
	}

	/**
	 * @方法说明 【客户信息】列表
	 */
	@PostMapping("list")
	@ApiOperation(value = "【客户信息】列表" , notes = "【客户信息】列表")
	@AutoLog("【客户信息】列表")
	public Result<List<Customer>> list(@RequestBody CustomerCond cond) {
		return Result.success(customerService.list(cond));
	}

	/**
	 * @方法说明 【客户信息】按主键查
	 */
	@PostMapping("findById")
	@ApiOperation(value = "【客户信息】按主键查" , notes = "【客户信息】按主键查")
	@AutoLog("【客户信息】按主键查")
	public Result<Customer> findById(@RequestParam("id") Integer id) {
		return Result.success(customerService.findById(id));
	}

	/**
	 * @方法说明 【客户信息】个数
	 */
	@PostMapping("count")
	@ApiOperation(value = "【客户信息】个数" , notes = "【客户信息】个数")
	@AutoLog("【客户信息】按主键查")
	public Result<Integer> count(@RequestBody CustomerCond cond) {
		return Result.success(customerService.count(cond));
	}
	/**
	 * @方法说明 【客户信息】导出
	 */
	@ApiOperation(value = "【客户信息】导出" , notes = "【客户信息】导出")
	@PostMapping("export")
	public void export(@RequestBody CustomerCond cond, HttpServletResponse response) throws IOException {
		customerService.export(cond,response);
	}
	
	/**
	 * @方法说明 【客户信息】导入
	 */
	@ApiOperation(value = "【客户信息】导入" , notes = "【客户信息】导入")
	@PostMapping("import")
	public Result<?> importExcel(@RequestPart("file") MultipartFile file) throws IOException {
		customerService.importExcel(file);
		return Result.success();
	}
}